package com.ruoyi.api.kehu;

import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.JiajiaMessage;
import com.ruoyi.system.domain.JiajiaOrder;
import com.ruoyi.system.domain.JiajiaShouHou;
import com.ruoyi.system.service.IJiajiaMessageService;
import com.ruoyi.system.service.IJiajiaOrderService;
import com.ruoyi.system.service.IJiajiaShouHouService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/shou/hou")
@Api(tags = "用户端售后相关")
public class ApiKndShouHouController {

    @Autowired
    private IJiajiaShouHouService shouHouService;

    @Autowired
    private IJiajiaOrderService orderService;

    @Autowired
    private IJiajiaMessageService messageService;

    @ApiOperation("我要售后")
    @PostMapping("/submitShouHou")
    public AjaxResult submitShouHou(@RequestParam Long orderId,
                                    @RequestParam @ApiParam(value = "选择的原因") String cause,
                                    @RequestParam @ApiParam(value = "备注") String des,
                                    @RequestParam @ApiParam(value = "图片") String urls,
                                    @RequestParam @ApiParam(value = "电话") String phone) {

        JiajiaOrder order = orderService.selectJiajiaOrderById(orderId);
        if (order == null)return AjaxResult.refreshAgain();
        if (order.getStatus().intValue() != 6)return AjaxResult.error("订单状态不对");
        if (order.getIsShouHou().intValue() == 1)return AjaxResult.error("此订单已申请过售后。");


        JiajiaShouHou shouHou = new JiajiaShouHou();
        shouHou.setUid(TokenUtil.getUid());
        shouHou.setOid(order.getId());
        shouHou.setWid(order.getWid());
        shouHou.setiName(order.getiName());
        shouHou.setCause(cause);
        shouHou.setDes(des);
        shouHou.setUrls(urls);
        shouHou.setPhone(phone);
        shouHouService.insertJiajiaShouHou(shouHou);

        JiajiaMessage message = new JiajiaMessage();
        message.setUid(TokenUtil.getUid());
        message.setTitle("系统消息");
        message.setContent("您的售后已提交，我们稍后会有专人联系您，请保持电话畅通!");
        message.setType(2);
        messageService.insertJiajiaMessage(message);

        order.setIsShouHou(1);
        orderService.updateJiajiaOrder(order);

        return AjaxResult.success("提交成功");
    }
}
